<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Companies extends BIT_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->header_views = array('admin/header');
		$this->footer_views = array('admin/footer');

		$this->view_data['message'] = "";
		
		// Check if user is loggedin
		if(!$this->isloggedin())
		{
			redirect('admin/login');
		}
	}
	private function isloggedin(){return (isset($this->session->userdata) && $this->session->userdata('loggedin')===TRUE);}


	public function index()
	{
		$companies = array();
		$branches = $this->db->query("SELECT DISTINCT name, id FROM branches WHERE parent > '0' ORDER BY name ASC");

		// Nest branches
		foreach ($branches->result() as $row)
		{
			$company = $row;
			$company->companies = $this->db->get_where('company',array('branch' => $row->id))->result();
			array_push($companies, $company);
		}

		$this->view_data['list'] = $companies;
		$this->v('admin/companies');


	}
	
	public function add_company($id = false)
	{
		$view_data["branchlists"] = $this->company->getNestedBranches();
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$form_rules = array(
					array(
						'field' => 'c_name',
						'label' => 'Company name',
						'rules' => 'trim|required'
					),
					array(
						'field' => 'c_branch',
						'label' => 'Bransch',
						'rules' => 'integer'
					)
		);

		$this->form_validation->set_rules($form_rules);
		
		if($this->form_validation->run() == FALSE)
		{
			$view_data['success'] = false;
			$view_data['company'] = false;
			$this->view_data['company_form'] = $this->load->view('admin/company_form', $view_data, true);
			$this->v('admin/add_company');	
		}
		else
		{

			$data = array(
				'id' 			=> $this->input->post('c_id'),
				'name' 			=> $this->input->post('c_name'),
				'orgnr'			=> $this->input->post('c_orgNumber'),
				'address' 		=> $this->input->post('c_address'),
				'short_desc' 	=> $this->input->post('c_description'),
				'phone' 		=> $this->input->post('c_telNumber'),
				'homepage' 		=> $this->input->post('c_website'),
				'email' 		=> $this->input->post('c_email'),
				'search_tags' 	=> $this->input->post('c_tags'),
				'branch' 		=> $this->input->post('c_branch'),
				'created' 		=> date('Y-m-d H:I:S'),
				'slug' 			=> $this->slugify($this->input->post('c_name')),
				'lastchangeby'	=> $this->session->userdata('userid')
			);
			
			$this->company->saveCompany($data);
			if($id)
			{
				redirect('/admin/companies/edit/' . $id);
			}
			else 
			{
				$view_data['company'] = false;
				$view_data["success"] = true;
				$this->view_data['company_form'] = $this->load->view('admin/company_form', $view_data, true);
				$this->v('admin/add_company');
			}
		}
	}
	
	public function edit_company($id = false)
	{
		if($id)
		{
			$view_data['success'] = false;
			$view_data['company'] = $this->company->findById($id);
			$view_data["branchlists"] = $this->company->getNestedBranches();
			$this->view_data['company_form'] = $this->load->view('admin/company_form', $view_data, true);
			$this->v('admin/edit_company');
		}
		else
		{
			$this->v('404');
		}
	}

	private function slugify($text)
	{ 
		# special accents
		$a = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','Ð','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','?','?','J','j','K','k','L','l','L','l','L','l','?','?','L','l','N','n','N','n','N','n','?','O','o','O','o','O','o','Œ','œ','R','r','R','r','R','r','S','s','S','s','S','s','Š','š','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Ÿ','Z','z','Z','z','Ž','ž','?','ƒ','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','?','?','?','?','?','?');
		$b = array('A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','IJ','ij','J','j','K','k','L','l','L','l','L','l','L','l','l','l','N','n','N','n','N','n','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','s','f','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','A','a','AE','ae','O','o');
		$text = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/','/[ -]+/','/^-|-$/'),array('','-',''),str_replace($a,$b,$text)));
		// replace non letter or digits by -
		$text = preg_replace('~[^\\pL\d]+~u', '-', $text);

		// trim
		$text = trim($text, '-');

		// transliterate
		$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

		// lowercase
		$text = strtolower($text);

		// remove unwanted characters
		$text = preg_replace('~[^-\w]+~', '', $text);

		if (empty($text))
		{
			return 'n-a';
		}

		return $text;
	}
}